import { Modal } from 'antd';
import { queryDictData, queryPerson } from '@/utils/common';
import AdSelect from '@/components/AdSelect';
import AdSearch from '@/components/AdSearch';
import prompt from '@/components/Prompt';

export const formate = 'YYYY-MM-DD HH:mm:ss';
export const formateNOSecond = 'YYYY-MM-DD HH:mm';
const { confirm } = Modal;

export const codes = {
  page: 'ARCHIVESPAGE',
  select: 'ARCHIVESPAGE_SELECT',
  showDetail: 'ARCHIVESPAGE_VIEW',
  add: 'ARCHIVESPAGE_ADD',
  addHK: 'ARCHIVESPAGE_ADDHK',
  confirm: 'ARCHIVESPAGE_CONFIRM',
  commit: 'ARCHIVESPAGE_COMMIT',
  pull: 'ARCHIVESPAGE_PULL',
  cancle: 'ARCHIVESPAGE_CANCLE',
  trial: 'ARCHIVESPAGE_TRIAL',
  edit: 'ARCHIVESPAGE_UPD',
  remove: 'ARCHIVESPAGE_DEL',
  addTrain: 'ARCHIVESPAGE_TRAIN_SAVE',
  editTrain: 'ARCHIVESPAGE_TRAIN_UPD',
  showDetailTrain: 'ARCHIVESPAGE_TRAIN_VIEW',
  addOil: 'ARCHIVESPAGE_OIL_SAVE',
  editOil: 'ARCHIVESPAGE_OIL_UPD',
  removeOil: 'ARCHIVESPAGE_OIL_DEL',
  showDetailOil: 'ARCHIVESPAGE_OIL_VIEW',
  addPressure: 'ARCHIVESPAGE_PRESSURE_SAVE',
  empty: 'ARCHIVESPAGE_PRESSURE_SAVE',
  toLocal: 'ARCHIVESPAGE_TOLOCAL',
  addYCD:'ARCHIVESPAGE_ADDYCD'
};
// UNSEALED 草稿、 SUBMITTED 已提交、 CONFIRM 已确认  CANCEL 作废
export const archivesStatusList = [
    { code: 'CANCEL', value: '作废' },
    { code: 'UNSEALED', value: '草稿' },
    { code: 'CONFIRM', value: '已确认' },
    { code: 'SUBMITTED', value: '已提交' },
];
//提派点： sendSome
export const sendSomeList = [{ code: '1', value: '1' }, { code: '2', value: '2' }];
// 车次类型
export const trainType = [
    { code: 'NORMAL', value: '正常运输' },
    { code: 'EMPTY', value: '短途放空' },
    { code: 'SHORT', value: '短途运输' },
    { code: 'LONG', value: '长途运输' },
    { code: 'LONGEMPTY', value: '长途放空' },
    { code: 'UNUSUAL', value: '异常运输' },
];
export const emptyTypeList = [
    { code: 'EMPTY', value: '放空补贴' },
    { code: 'NIGHTSHIFT', value: '过夜补贴' },
    { code: 'CABINET', value: '深圳柜补贴' },
    { code: 'WEEKEND', value: '周末开工补贴' },
];
export const driverType = [
    { code: 'HW01', value: '港车司机' },
    { code: 'GD01', value: '骑师' },
    
];

export const allUrl = {
    companyList: 'tms/tms-bill/selectSpplierList_s',
    userList: 'tms/tms-bill/selectUserList_s',
    supplierList: 'tms/tms-bill/selectSpplierList_s',
    supplierDetail: 'mds/d-customer/viewCompany',
    userDetail: 'mds/mds-user/viewDetails',
};

export const routeUrl = {
    add: '/carAccount/ArchivesList/addArchives',
    addOneTeam: '/carAccount/ArchivesListYCD/addArchivesOneTeam',
    edit: '/carAccount/ArchivesList/editArchives',
    editOneTeam: '/carAccount/ArchivesListYCD/editArchivesOneTeam',
    editHK: '/carAccount/ArchivesListHK/editArchivesHK',
    editEmpty: '/carAccount/ArchivesListHK/editArchivesEmpty',
};

export const allDispatchType = {
    // 行车档案
    list: 'Archives/selectArchivesList',
    detail: 'Archives/selectArchivesDetail',
    remove: 'Archives/removeArchives',
    confirm: 'Archives/confirmArchives',
    commit: 'Archives/commitArchives',
    operate: 'Archives/archivesOperate',
    oneTeamoperate: 'Archives/archivesOneTeamOperate',

    trial: 'Archives/trialFeeArchives',
    cancel: 'Archives/cancelArchives',
    pull: 'Archives/pullArchivesByP2',
    emptyArchives: 'Archives/emptyArchivesOperate',
    selectEmptyDetails: 'Archives/selectEmptyDetails',
    toLocal: 'Archives/toLocal',
    //车次
    trainInfoList: 'Archives/trainInfoList',
    trainInfoDetails: 'Archives/trainInfoDetails',
    trainInfoOperate: 'Archives/trainInfoOperate',
    trainInfoOneTeamOperate: 'Archives/trainInfoOneTeamOperate',
    deleteTrains: 'Archives/deleteTrains',
    getTrainNosToSZ: 'Archives/getTrainNosToSZ',
    //杂费
    incidentalInfoList: 'Archives/incidentalInfoList',
    pullIncidentals: 'Archives/pullIncidentals',
    //根据杂费 费用类型获取科目
    getCostAccount:'Archives/getCostAccount',
    //加油
    oilInfoList: 'Archives/oilInfoList',
    oilInfoDetails: 'Archives/oilInfoDetails',
    oilInfoOperate: 'Archives/oilInfoOperate',
    oilBindInfoList: 'Archives/oilBindInfoList',
    oilBindInfoOperate: 'Archives/oilBindInfoOperate',
    removeOilInfo: 'Archives/removeOilInfo',
    oilCardList: 'Archives/oilCardList',
    calHundredConsump: 'Archives/calHundredConsump',
    // 压夜
    pressureInfoList: 'Archives/pressureInfoList',

    ownCompany: 'component/queryOwnCompany',
    value: 'Archives/allValus',
    selectFileList: 'Archives/selectFileList',
    getStartMileage: 'Archives/getStartMileage',
    getEtcStartAmount: 'Archives/getEtcStartAmount',
   
};

export function renderTableAdSelect({ key, data, value, props, type }) {
    let params = { onlyRead: true, value };
    // console.log('value---',value,data)

    if (key) {
        const { dictObject } = props;
        params = { data: dictObject[key], payload: { code: [key] }, ...params };
    } else {
        params = { data, ...params };
    }
    let show = { id: 'code', name: 'value' };
    if (type == 'train') {
        show = { id: 'id', name: 'trainNo' };
    }
    params.show = show;
    return <AdSelect {...params} />;
}

export function renderTableAdSearch({ value, props }) {
    if (!value || !searchValue) return '';
    const { searchValue } = props;
    const params = {
        onlyRead: true,
        value: searchValue[value],
        label: 'loginName',
        name: 'sysName',
    };
    return <AdSearch {...params} />;
}

export function selectArchivesList({ payload = {}, props } = {}) {
  const { dispatch, searchValue } = props;
 
  dispatch({
    type: allDispatchType.list,
    payload,
    callback: data => {
     // console.log('datadatadata',data)
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
    },
  });
}

export function selectArchivesDetailAndInfo({ type, payload, props, callback }) {
    const { dispatch } = props;
    dispatch({
        type,
        payload,
        callback: data => {
            if (!data) return;
            callback && callback(data);
        },
    });
}

export function saveAllValues({ payload = {}, props } = {}) {
    const { dispatch } = props;
    dispatch({
        type: 'Archives/allValus',
        payload,
    });
}

//时间判断：  date1:小日期   date2:大日期
export function DateMinus(date1, date2) {
    if (date1 && date2) {
        var sdate = new Date(date1);
        var now = new Date(date2);
        var days = Number(now.getTime()) - Number(sdate.getTime());
        var day = parseFloat(days / (1000 * 60 * 60 * 24));
        return day.toString();
    }
}
/*
   *  确认车次时需进行校验：
      1）ETC发车金额如不为空或0,则收车金额不允许为空或0
      2）车次字段不能为空
 */
export function checkConfirm({ props, id, type, isHK, callback }) {
    const {
        trainInfoList,
        oilInfoList,
        pressureInfoList,
        form: { getFieldValue },
    } = props;
    let tipsText = '';

    const trainList = trainInfoList[id] ? trainInfoList[id].list || [] : [];
    const pressureList = pressureInfoList[id] ? pressureInfoList[id].list || [] : [];
    const oilList = oilInfoList[id] ? oilInfoList[id].list || [] : [];
    const etcSA = getFieldValue('etcStartAmount');
    const etcEA = getFieldValue('etcEndAmount');
    if (etcSA && !etcEA) {
        tipsText = '收车余额 不允许为空或0；';
    }
    // console.log('id=======', id, 'trainInfoList', trainInfoList);
    //校验行车档案
    const unit = {
        checkFields: (list, fields, textType) => {
            let textFields = '';
            list.map((item, i) => {
                let tipsStr = [];
                for (let k in item) {
                    if (!item[k] && fields[k]) {
                        tipsStr.push(fields[k]);
                    }
                }
                if (tipsStr.length > 0) {
                    // tipsText = `${tipsText}${textType}序号${i + 1}：${tipsStr.join(',')}不允许为空；`;
                    textFields = `${textFields}序号${i + 1}：${tipsStr.join(',')}不允许为空；`;
                }
            });
            if (textFields) {
                tipsText = tipsText + `<div>${textType}${textFields}</div>`;
            }
        },
        titleTips: val => {
            switch (val) {
                case 'confirm':
                    return '确认';
                    break;
                case 'trial':
                    return '试算';
                    break;
                case 'commit':
                    return '提交';
                    break;
                default:
                    return;
            }
        },
    };
    //校验车次
    // console.log('isHK===校验', isHK)
    if (!isHK) {
        if (trainList.length == 0) {
            tipsText = tipsText + '车次数据不能为空。';
        } else {
            const fields = {
                trainNo: '车次号',
                weight: '货重',
                cabinetType: '柜型',
                cartHanging: '车挂',
                driverName: '司机',
                origin: '始发地',
                destination: '目的地',
                transportStartTime: '发车时间',
                transportEndTime: '收车时间',
                startMileage: '发车里程',
                collectMileage: '收车里程',
            };
            // unit.checkFields(trainList, fields, '车次信息：');
        }
    }
    if (!isHK) {
        if (oilList.length == 0) {
            tipsText = tipsText + '加油数据不能为空。';
        } else {
            const fields = {
                oilAmount: '加油量',
            };
            unit.checkFields(oilList, fields, '加油记录：');
        }
    }

    if (pressureList.length > 0) {
        const fields = {
            driverName: '司机',
        };
        if (!isHK) fields.trainId = '车次号';
        unit.checkFields(pressureList, fields, '压夜记录：');
    }

    if (tipsText) {
        prompt({
            content: (
                <div dangerouslySetInnerHTML={{ __html: `${unit.titleTips(type)}时：` + tipsText }}></div>
            ),
            type: 'error',
        });
        return;
    }
    if (type == 'commit') {
        confirm({
            title: '确认提交？',
            content: ' ',
            okText: '确定',
            okType: 'danger',
            cancelText: '取消',
            onOk: () => {
                callback && callback();
            },
        });
    } else {
        callback && callback();
    }
}

export const columnsTrain = [
    {
        title: '车次号',
        dataIndex: 'trainNo',
        width: '100%',
    },
];

// INLAND("INLAND", "国内车（默认）"),
// HK("HK", " 中港车-过港"),
// HK_NONSTOP("HK_NONSTOP","中港车-直达"),
// LOCAL("LOCAL","本地local"),
// EMPTY("EMPTY","放空"),
// ONE_TEAM("ONE_TEAM","一车队");
